package com.example.demo.enity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * 1.用户基本信息
 * 2.用户安全登录
 * @author limeng
 *
 */
public class UserInfo implements UserDetails{
    
	//private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Integer uid;

    private String utel;

    private String uname;

    private String upassword;

    private Boolean ulocked;

    private String diqu;

    private Integer fcount;

    private Boolean uenable;

    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd",timezone = "GMT+8")
    private Date time;
    //封装用户的角色
    private List<RoleInfo> roles;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUtel() {
        return utel;
    }

    public void setUtel(String utel) {
        this.utel = utel == null ? null : utel.trim();
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname == null ? null : uname.trim();
    }

    public String getUpassword() {
        return upassword;
    }

    public void setUpassword(String upassword) {
        this.upassword = upassword == null ? null : upassword.trim();
    }

    public Boolean getUlocked() {
        return ulocked;
    }

    public void setUlocked(Boolean ulocked) {
        this.ulocked = ulocked;
    }

    public String getDiqu() {
        return diqu;
    }

    public void setDiqu(String diqu) {
        this.diqu = diqu == null ? null : diqu.trim();
    }

    public Integer getFcount() {
        return fcount;
    }

    public void setFcount(Integer fcount) {
        this.fcount = fcount;
    }

    public Boolean getUenable() {
        return uenable;
    }

    public void setUenable(Boolean uenable) {
        this.uenable = uenable;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<SimpleGrantedAuthority> authorities =new ArrayList<>();
		for(RoleInfo role:roles)
		{
			authorities.add(new SimpleGrantedAuthority(role.getRname()));
		}
		return authorities;
	}

	
	  @Override
	 public String getPassword() { // TODO Auto-generated method stub
	 return upassword;
	 }
	 
	 @Override 
	 public String getUsername() { // TODO Auto-generated method stub
	return uname; 
	}
	 
	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return !ulocked;
	}

	public List<RoleInfo> getRoles() {
		return roles;
	}

	public void setRoles(List<RoleInfo> roles) {
		this.roles = roles;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}


}